﻿using System;
using System.Collections;
using System.IO;
using System.Text;

namespace DeYi.NuGet.Other
{
    public class LogHelper
    {
        /// <summary>
        /// 写运行日志
        /// </summary>
        /// <param name="strCont">内容</param>
        /// <param name="type">类型</param>
        /// <param name="appendToLast">是否追加到末尾</param>
        /// <returns></returns>
        public static void RunLog(string strCont, string type = "", bool appendToLast = true)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/Log/";
            string filename = DateTime.Now.ToString("yyyy年MM月dd日") + "Run.log";
            if (type != "")
            {
                filename = DateTime.Now.ToString("yyyy年MM月dd日") + "_" + type + ".log";
            }
            WriteLog(path, filename, strCont);
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="strCont">内容</param>
        /// <param name="type">类型</param>
        /// <param name="appendToLast">是否追加到末尾</param>
        /// <returns></returns>
        public static void ErrorLog(string strCont, string type = "", bool appendToLast = true)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/Log/";
            string filename = DateTime.Now.ToString("yyyy年MM月dd日") + "Error.log";
            if (type != "")
            {
                filename = DateTime.Now.ToString("yyyy年MM月dd日") + "_" + type + ".log";
            }
            WriteLog(path, filename, strCont);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="path">路径 </param>
        /// <param name="fileName">文件名</param>
        /// <param name="content">写入的内容</param>
        /// <param name="appendToLast">是否将内容添加到未尾,默认添加</param>
        public static void WriteLog(string path, string fileName, string content, bool appendToLast = true)
        {
            try
            {
                if (!Directory.Exists(path))//如果不存在就创建file文件夹
                    Directory.CreateDirectory(path);

                using (FileStream stream = File.Open(path + fileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] by = Encoding.Default.GetBytes(content);
                    if (appendToLast)
                    {
                        stream.Position = stream.Length;
                    }
                    else
                    {
                        stream.SetLength(0);
                    }
                    stream.Write(by, 0, by.Length);
                }
            }
            catch (Exception)
            {
                //NoToDO
            }
        }

        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="sPath">    年月  例  2011-04</param>
        /// <param name="sFileName">月日  例  04-22</param>
        /// <param name="content">时间+  内容</param>
        /// <returns></returns>
        public static bool WriteLog(string sPath, string sFileName, string content)
        {
            try
            {

                if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
                string v_filename = sPath + "\\" + sFileName;

                StreamWriter sw = File.AppendText(v_filename);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + content);
                sw.Close();

                return true;

            }
            catch { return false; }
        }

    }
}
